package com.example.meituan.activity.user.adapter;

import static androidx.core.content.ContextCompat.startActivity;
import static java.security.AccessController.getContext;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.meituan.MainActivity;
import com.example.meituan.R;
import com.example.meituan.activity.bean.ImgBean;
import com.example.meituan.activity.bean.ShopBean;
import com.example.meituan.activity.man.UpdateShopActivity;
import com.example.meituan.activity.man.frament.OnItemClickListener;
import com.example.meituan.activity.user.farment.OnProductClickListener;
import com.example.meituan.dao.AdminDao;
import com.example.meituan.dao.ImgDao;
import com.example.meituan.dao.ShopDao;

import java.io.File;
import java.util.List;

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder> {

    private List<ShopBean> productList;


    private static OnProductClickListener listener;

    private static OnItemClickListener listener2;

    public ProductAdapter(List<ShopBean> productList, OnProductClickListener listener) {
        this.productList = productList;
        this.listener = listener;
    }

    public ProductAdapter(List<ShopBean> productList) {
        this.productList = productList;
    }

    public ProductAdapter(List<ShopBean> productList, OnItemClickListener listener) {
        this.productList = productList;
        this.listener2 = listener;
    }

    public ProductAdapter(List<ShopBean> list, OnProductClickListener listener, OnItemClickListener listener2) {
        this.productList = list;
        this.listener = listener;
        this.listener2 = listener2;
    }

    @NonNull
    @Override
    public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_shop_list, parent, false);
        return new ProductViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ProductViewHolder holder, int position) {
        ShopBean product = productList.get(position);
        holder.bind(product);
    }

    @Override
    public int getItemCount() {
        return productList.size();
    }

    public static class ProductViewHolder extends RecyclerView.ViewHolder {
        ImageView productImage;
        TextView productTitle, productSubtitle, productPrice, productSold,productShop;
        LinearLayout linearLayout;
        public ProductViewHolder(@NonNull View itemView) {
            super(itemView);
            productImage = itemView.findViewById(R.id.productImage);
            productTitle = itemView.findViewById(R.id.productTitle);
            productSubtitle = itemView.findViewById(R.id.productSubtitle);
            productPrice = itemView.findViewById(R.id.productPrice);
            productSold = itemView.findViewById(R.id.productSold);
            productShop=itemView.findViewById(R.id.productShop);

        }

        public void bind(ShopBean product) {
            itemView.setOnClickListener(v -> {
                if (listener2 != null) {
                    listener2.onItemClick(product);
                }
            });
            List<ImgBean>imgBeans= ImgDao.getAllImagesByShopId(product.getId());
            if (imgBeans != null && !imgBeans.isEmpty()) {
                ImgBean firstImgBean = imgBeans.get(0);
                String imagePath = firstImgBean.getImg();

                File imageFile = new File(imagePath);
                if (imageFile.exists()) {

                    Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
                    if (bitmap != null) {
                        productImage.setImageBitmap(bitmap);
                    }

                }
            }
            productTitle.setText(product.getName());
            productSubtitle.setText(product.getDes());
            productPrice.setText("￥"+product.getPrice());
            productSold.setText("月销量:"+ShopDao.getMonSale(product.getId()));
            productShop.setText(AdminDao.getBusiness(product.getBusinessId()).getsName());
            productShop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onProductClick(product.getBusinessId());
                    }
                }
                }
            );
        }
    }


}